﻿@*角色授权页面*@

@{
    Layout = "_Layout";
    string ids = (string)ViewData["ids"];
}
<div style="margin: 15px;">
    <form class="layui-form" id="updateForm" action="@Url.Action("SavePermission","Role")">
        <div class="layui-form-item">
            <label class="layui-form-label">选择资源</label>
            <div class="layui-input-block">
                <ul id="authTree" class="ztree"> </ul>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="btnSubmit">提交</button>
            </div>
        </div>
    </form>
    <script type="text/javascript">
        function vModel() {
            this.ids = ko.observable("");
            this.roles = ko.observable("");
        }
        var model = new vModel();
        layui.use(['element', 'jquery', 'form','ztree'], function () {
            var $ = layui.jquery,
                form = layui.form,
                ztree = layui.ztree;
            $(document).ready(function () {
                initTree();
            });
            var zTreeObj;
            var initTree = function () {
                var setting = {
                    view: { selectedMulti: false },
                    check: {
                        enable: true,
                        chkboxType:{ "Y": "p", "N": "ps" }
                    },
                    data: {
                        key: {
                            name: 'text',
                            title: 'text'
                        },
                        simpleData: {
                            enable: true,
                            idKey: 'id',
                            pIdKey: 'parent'
                        }
                    },
                    callback: {
                        onCheck: onCheck
                    }
                };
                $.ajax({
                    type: "Get",
                    url: "/Role/GetMenusByRole?ids=@ids", //获取数据的ajax请求地址
                    success: function (res) {
                        if (!res.success) {
                            admin_error(res.msg);
                            return false;
                        }
                        zTreeObj = $.fn.zTree.init($("#authTree"), setting, res.data);
                        zTreeObj.expandAll(true);
                        model.ids('@ids');
                        var nodes = zTreeObj.getCheckedNodes(true);
                        var nodeIds = '';
                        for (var i = 0; i < nodes.length; i++) {
                            nodeIds += nodes[i].id + ",";
                        }
                        model.roles(nodeIds);
                        console.log(model.roles());
                    }
                });
            };
            function onCheck(e, treeId, treeNode) {
                var nodes = zTreeObj.getCheckedNodes(true);
                var nodeIds = '';
                for (var i = 0; i < nodes.length; i++) {
                    nodeIds += nodes[i].id + ",";
                }
                model.roles(nodeIds);
                console.log(model.roles());
            }
            //监听提交
            form.on('submit(btnSubmit)', function (data) {
                $.ajax({
                    url: $("#updateForm").attr("action"),
                    type: 'post',
                    dataType: 'json',
                    async: false,
                    data:model,//$("#updateForm").serialize(),
                    success: function (res) {
                        if (res.success) {
                            admin_success();
                            parent.layer.closeAll('iframe');
                        } else {
                            admin_error(res.msg);
                        }
                    }
                });
                return false;
            });
        });
        ko.applyBindings(model);//注册vModel
    </script>
</div>